﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Air_Trafic_Control.Domain.Entity;
using Air_Trafic_Control.Domain.Control;
using Air_Trafic_Control.Domain.Common;

namespace Air_Trafic_Control
{
    public partial class NewGame : Form
    {
        private User m_selectedUser;
        private Game m_newGame;
        private bool m_newUser = false;
        private EAT m_eat;

        //public delegate void FormCompleted();
        //public event FormCompleted FormCompletedEvent;
        
        /// <summary>
        /// Creates a new NewGame form. The calling object should register an event listener with the FormCompletedEvent so it can retrieve the SelectedUser and NewGame.
        /// </summary>
        public NewGame()
        {
            InitializeComponent();

            this.m_eat = EAT.GetEAT();

            foreach (User u in this.m_eat.GetUsers())
            {
                this.cmbUserName.Items.Add(u);
            }

            if (this.cmbUserName.Items.Count > 0)
            {
                this.cmbUserName.SelectedItem = this.cmbUserName.Items[0];
            }

            for (int i = Constants.GAME_MIN_DIFFICULTY; i != Constants.GAME_MAX_DIFFICULTY; i++)
            {
                Int32 nextItem = new Int32();
                nextItem = i;

                this.cmbStartingLevel.Items.Add(nextItem);
            }

            if (this.cmbStartingLevel.Items.Count > 0)
            {
                this.cmbStartingLevel.SelectedItem = this.cmbStartingLevel.Items[0];
            }

            this.Shown += new EventHandler(NewGame_Shown);
            this.VisibleChanged += new EventHandler(NewGame_Shown);
        }

        #region Public methods

        /// <summary>
        /// Accessor for the selected user.
        /// </summary>
        public User SelectedUser
        {
            get
            {
                return this.m_selectedUser;
            }
        }

        /// <summary>
        /// Accessor for the new game.
        /// </summary>
        public Game CreatedGame
        {
            get
            {
                return this.m_newGame;
            }
        }

        /// <summary>
        /// Allows access to the new Game's name.
        /// </summary>
        public string CreatedGameName
        {
            get
            {
                return this.txtGameName.Text;
            }
        }

        /// <summary>
        /// Resets the form for use again.
        /// </summary>
        public void Reset()
        {
            this.rbExistingUser.Select();
            this.cmbUserName.Visible = true;
            this.txtUserName.Visible = false;
            this.m_newUser = false;

            this.cmbUserName.Items.Clear();

            foreach (User u in this.m_eat.GetUsers())
            {
                this.cmbUserName.Items.Add(u);
            }

            if (this.cmbUserName.Items.Count > 0)
            {
                this.cmbUserName.SelectedItem = this.cmbUserName.Items[0];
            }

            this.txtUserName.Text = "";
            this.txtGameName.Text = "";
        }

        #endregion

        #region Private Methods

        private void NewGame_Shown(object sender, EventArgs e)
        {
            if (this.cmbUserName.Items.Count < 1)
            {
                this.rbNewUser.Select();
                this.txtUserName.Visible = true;
                this.cmbUserName.Visible = false;
                this.m_newUser = true;

                this.rbExistingUser.Enabled = false;
            }
            else
            {
                this.rbExistingUser.Enabled = true;
            }
        }

        /// <summary>
        /// Handles creating a new user if that option is selected.
        /// </summary>
        /// <returns>True if operations were successful.</returns>
        private bool CreateNewUser()
        {
            User newUser = this.m_eat.CreateNewUser(this.txtUserName.Text);

            if (newUser == null)
            {
                MessageBox.Show("This User Name is already taken. Please use another.", "Non-Unique User Name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            this.m_selectedUser = newUser;

            return true;
        }

        /// <summary>
        /// Handles creating a new game.
        /// </summary>
        /// <returns>Ture if operations were successful.</returns>
        private bool CreateNewGame()
        {
            bool success = true;

            Game newGame = new Game(this.txtGameName.Text);
            newGame.DifficultyLevel = (Int32)this.cmbStartingLevel.SelectedItem;

            success = this.m_eat.CreateNewGame(this.m_selectedUser, newGame);

            if (!(success))
            {
                MessageBox.Show("This Game Name is already taken. Please use another.", "Non-Unique Game Name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            this.m_newGame = newGame;

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            bool success = true;
            if (this.m_newUser)
            {
                if (this.txtUserName.Text.Length < 1)
                {
                    MessageBox.Show("A User Name is required.", "Invalid User Name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    success = false;
                }
            }
            else
            {
                this.m_selectedUser = (User)this.cmbUserName.SelectedItem;
                this.m_eat.SelectedUser = this.m_selectedUser;
            }

            if (success)
            {
                if (this.txtGameName.Text.Length < 1)
                {
                    MessageBox.Show("A Game Name is required.", "Invalid Game Name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    success = false;
                }
            }

            if(success && this.m_newUser)
            {
                success = this.CreateNewUser();
                this.m_eat.SelectedUser = this.m_selectedUser;

                if(!success)
                {
                    MessageBox.Show("An error occurred while creating your new User.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            if (success)
            {
                success = this.CreateNewGame();
                
                this.m_eat.SelectedGame = this.CreatedGame;
                this.m_eat.SelectedUser = this.SelectedUser;
                MessageBox.Show(this.CreatedGameName);
                this.Reset();
                this.Hide();
            }
        }

        private void rbExistingUser_CheckedChanged(object sender, EventArgs e)
        {
            this.cmbUserName.Visible = true;
            this.txtUserName.Visible = false;
            this.m_newUser = false;
        }

        private void rbNewUser_CheckedChanged(object sender, EventArgs e)
        {
            this.txtUserName.Visible = true;
            this.cmbUserName.Visible = false;
            this.m_newUser = true;
        } 

        #endregion
        
    }
}
